<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>油品修改工具</title>
<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/themes/gray/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/themes/icon.css">
<script type="text/javascript" src="<%=path%>/easyui/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=path%>/easyui/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=path%>/easyui/js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="<%=path%>/easyui/js/con_isok.js"></script>

</head>
<body class="easyui-layout">

	<div data-options="region:'north',border:false" style="padding:2px;">
		<table align="">
			<tr>
				<td>IP地址:</td>
				<td><input id="ip_address_id" class="easyui-validatebox" data-options="required:true" value="">
				</td>

				<td>出库单号</td>
				<td><input id="ckd_no_id" class="easyui-numberbox" data-options="required:true" value="">
				</td>

				<td>
					<b id="node_name_id"></b>
				</td>
				<td>
					<b id="node_no_id"></b>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center"><a id="select_btn" href="javascript:void(0);" class="easyui-linkbutton" onclick="select_data1();">查询</a></td>
				<td colspan="2" align="center"><a id="select_btn" href="javascript:void(0);" class="easyui-linkbutton" onclick="update1();">保存</a></td>
			</tr>
		</table>
	</div>

	<div data-options="region:'center', border:false" style="padding:2px;">
		<table id="dg" class="easyui-datagrid" data-options="
			url:'<%=path%>/oils/index.do',
			fit:true,
			singleSelect:true,
			fitColumns: true,
			border:false,
			onBeginEdit:onBeginEdit,
			onLoadSuccess: onLoadSuccess
	">
			<thead>
				<tr>
					<th data-options="field:'ckdNo',width:100">出库单号</th>
					<th data-options="field:'pingtaiOilsNo',width:100">中心平台油品编码</th>
					<th data-options="field:'pingtaiOilsName',width:100">中心平台油品名称</th>
					<th data-options="field:'bfCenterOilsNo',width:100">北分中心油品编码</th>
					<th data-options="field:'appOilsNo',width:100">站油品编码</th>
					<th data-options="field:'appOilsName',width:100">站油品名称</th>
					<th data-options="field:'yanshouState',width:100">验收状态</th>
					<th data-options="field:'oilsList',width:100,editor:{type:'combobox', options:{required: true,valueField:'id', textField:'text', editable:false }}">油品列表</th>
				</tr>
			</thead>
		</table>
	</div>

	<script type="text/javascript">
		
		function onBeginEdit(index, row){
			if(row.pingtaiOilsNo){
				$.ajax({
					url: "<%=path %>/oils/oilsRef.do",
					type: "post",
					dataType: "json",
					success:function(data){
						var ed = $('#dg').datagrid('getEditor', {index: index, field: 'oilsList'});
						$(ed.target).combobox('loadData', data[row.pingtaiOilsNo]);
					}
				});
			}
		}
	
		function onLoadSuccess(){
			$("#dg").datagrid("selectRow", 0);
			var row = $("#dg").datagrid("getSelected");
			if(row){
				$('#dg').datagrid('beginEdit', 0);
				getNodeInfo();
			}
		}
		
		
		function getNodeInfo(){
			var ip = $("#ip_address_id").val();
			if(ip){
				$.ajax({
					url: "<%=path %>/jhsy/nodeInfo.do",
					type: "post",
					dataType: "json",
					data:{
						ipAddress:ip
					},
					success:function(data){
						$("#node_name_id").html(data["nodeName"]);
						$("#node_no_id").html(data["nodeno"]);
					}
				});
			}
		}
		
		
		
		function isRightIP(value) {
			var ip_rex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
			return ip_rex.test(value);
		}
		
		function select_data1(){
			var ipAddress = $("#ip_address_id").val();	
			if(!ipAddress){
				$.messager.alert("提示","请输入IP地址!", "error");
				return;
			}
			if(!isRightIP(ipAddress)){
				$.messager.alert("提示","IP地址错误!", "error");
				return;
			}
			var ckdNo = $("#ckd_no_id").val();	
			if(!ckdNo){
				$.messager.alert("提示","请输入出库单号!", "error");
				return;
			}
			isOkCon(ipAddress, "<%=path %>","select_data()");
		}
		
		
		function select_data(){
			
			var ipAddress = $("#ip_address_id").val();	
			if(!ipAddress){
				$.messager.alert("提示","请输入IP地址!", "error");
				return;
			}
			if(!isRightIP(ipAddress)){
				$.messager.alert("提示","IP地址错误!", "error");
				return;
			}
			
			var ckdNo = $("#ckd_no_id").val();	
			if(!ckdNo){
				$.messager.alert("提示","请输入出库单号!", "error");
				return;
			}
			
			
			$("#dg").datagrid('load', {
				ckdNo:ckdNo,
				ipAddress:ipAddress
			});
			
		}
		
		function update1(){
			var ipAddress = $("#ip_address_id").val();	
			if(!ipAddress){
				$.messager.alert("提示","请输入IP地址!", "error");
				return;
			}
			if(!isRightIP(ipAddress)){
				$.messager.alert("提示","IP地址错误!", "error");
				return;
			}
			
			var ckdNo = $("#ckd_no_id").val();	
			if(!ckdNo){
				$.messager.alert("提示","请输入出库单号!", "error");
				return;
			}
			
			
			var row = $("#dg").datagrid("getSelected");
			if(!row){
				$.messager.alert("提示","请选中一行数据!", "error");
				return;
			}
			
			
			if(row.yanshouState == "已开始"){
				$.messager.alert("提示","该条数据已经开始进货验收,不能修改!", "error");
				return;
			}
			
			if (!$('#dg').datagrid('validateRow', 0)){
				$.messager.alert("提示","请将数据填写完整!", "error");
				return;
			}
			
			isOkCon(ipAddress, "<%=path %>","update()");
		}
		
		function update(){
			var ipAddress = $("#ip_address_id").val();	
			if(!ipAddress){
				$.messager.alert("提示","请输入IP地址!", "error");
				return;
			}
			if(!isRightIP(ipAddress)){
				$.messager.alert("提示","IP地址错误!", "error");
				return;
			}
			
			var ckdNo = $("#ckd_no_id").val();	
			if(!ckdNo){
				$.messager.alert("提示","请输入出库单号!", "error");
				return;
			}
			
			
			var row = $("#dg").datagrid("getSelected");
			if(!row){
				$.messager.alert("提示","请选中一行数据!", "error");
				return;
			}
			
			
			if(row.yanshouState == "已开始"){
				$.messager.alert("提示","该条数据已经开始进货验收,不能修改!", "error");
				return;
			}
			
			if (!$('#dg').datagrid('validateRow', 0)){
				$.messager.alert("提示","请将数据填写完整!", "error");
				return;
			}
			
			$('#dg').datagrid('endEdit', 0);
			
			$.ajax({
				url: "<%=path%>/oils/update.do",
				type: "post",
				dataType: "json",
				data:{
					ckdNo: ckdNo,
					ipAddress:ipAddress,
					oilNo: row.oilsList
				},
				success:function(data){
					if(data){
						$.messager.alert("提示","修改成功!", "info");
					}else{
						$.messager.alert("提示","修改失败!", "error");
					}
				}
			});
		}
		
	</script>
</body>
</html>